﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyKho.GUI.Library;
using System.IO;
using System.Collections.Specialized;
using System.Threading;

namespace QuanLyKho.GUI
{
    public partial class frmRestore : Form
    {

        private static string dbName = string.Empty, serverName = string.Empty;
        private BackgroundWorker bgWork;

        private string filename = "", duongdan = "", urlMDF = "", urlLDF = "";
        private clsDbTool objDbTool = null;
        private StringCollection strColl;

        public frmRestore()
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
            dbName = frmLogin.dbName;
            serverName = frmLogin.tenmaychu;
            duongdan = frmConnect.duongdan;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        void bgWork_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            label2.Visible = false;
            pictureBox1.Visible = false;
        }

        void bgWork_DoWork(object sender, DoWorkEventArgs e)
        {
            //throw new NotImplementedException();
            try
            {
                label2.Text = "Hệ thống đang phục hồi CSDL...Vui lòng chờ trong giây lát.";
                objDbTool.RestoreDatabase(dbName, filename, dbName, dbName + "_log", urlMDF, urlLDF);
                label2.Text = "Restore Database thành công.";
                Thread.Sleep(2000);
                label2.Text = "Hệ thống sẽ tự khởi động lại để cập nhật dữ liệu...";
                Thread.Sleep(3000);
                Application.Restart();
            }
            catch (Exception ex)
            {
                label2.Text = "Có lỗi xảy ra trong quá trình Restore CSDL";
            }
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {
            objDbTool = new clsDbTool(serverName);
            if (dbName.Length > 0)
                objDbTool.KillAllProcess(dbName);

            filename = new clsLibrary().SelectTextFile(@"C:\", false);
            if (!string.IsNullOrEmpty(filename))
            {
                if (!Directory.Exists(duongdan))
                {
                    DirectoryInfo di = new DirectoryInfo(duongdan);
                    di.Create();
                    di.Attributes = FileAttributes.System;
                }

                urlMDF = duongdan + dbName + ".mdf";
                urlLDF = duongdan + dbName + "_log.ldf";

                bgWork = new BackgroundWorker();
                bgWork.RunWorkerAsync();
                bgWork.DoWork += new DoWorkEventHandler(bgWork_DoWork);
                bgWork.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWork_RunWorkerCompleted);
                label2.Visible = true;
                pictureBox1.Visible = true;
            }
        }
    }
}
